<?php
/**
 * Created by PhpStorm.
 * User: fyyo4
 * Date: 2016/12/30
 * Time: 22:11
 */

namespace mrcn;


class DaApi
{
	private $host, $pass, $user, $port, $usessl;

	public function __construct($DAHost, $DAPort = 2222, $DAUser, $DAPass, $SSL = false)
	{
		$this->host   = $DAHost;
		$this->pass   = $DAPass;
		$this->user   = $DAUser;
		$this->port   = $DAPort;
		$this->usessl = $SSL;
	}

	public function createUser($username, $password, $packge, $domain, $email, $ip)
	{
		$command            = 'CMD_API_ACCOUNT_USER';
		$fields['action']   = 'create';
		$fields['add']      = 'Submit';
		$fields['username'] = $username;
		$fields['email']    = $email;
		$fields['passwd']   = $password;
		$fields['passwd2']  = $password;
		$fields['domain']   = $domain;
		$fields['package']  = $packge;
		$fields['ip']       = $ip;
		$fields['notify']   = 'no';
		return $this->run($command, $fields);
	}

	public function createReseller($username, $password, $packge, $domain, $email, $ip)
	{
		$command            = 'CMD_ACCOUNT_RESELLER';
		$fields['action']   = 'create';
		$fields['add']      = 'Submit';
		$fields['username'] = $username;
		$fields['email']    = $email;
		$fields['passwd']   = $password;
		$fields['passwd2']  = $password;
		$fields['domain']   = $domain;
		$fields['package']  = $packge;
		$fields['ip']       = $ip;
		$fields['notify']   = 'no';
		return $this->run($command, $fields);
	}

	public function createAdmin($username, $password, $email)
	{
		$command            = 'CMD_ACCOUNT_RESELLER';
		$fields['action']   = 'create';
		$fields['username'] = $username;
		$fields['email']    = $email;
		$fields['passwd']   = $password;
		$fields['passwd2']  = $password;
		$fields['notify']   = 'no';
		return $this->run($command, $fields);
	}

	public function createDomainForCurrentUser($domain, $bandwidth = -1, $quota = -1, $ssl, $cgi, $php)
	{
		$command          = 'CMD_API_DOMAIN';
		$fields['action'] = 'create';
		$fields['domain'] = $domain;
		if ($bandwidth == -1) {
			$fields['bandwidth']  = 2333333;
			$fields['ubandwidth'] = "unlimited";
		} else {
			$fields['bandwidth']  = $bandwidth;
			$fields['ubandwidth'] = "false";
		}
		if ($quota == -1) {
			$fields['quota']  = 2333333;
			$fields['uquota'] = "unlimited";
		} else {
			$fields['quota']  = $quota;
			$fields['uquota'] = "false";
		}
		if ($ssl)
			$fields['ssl'] = "ON";
		else
			$fields['ssl'] = "OFF";
		if ($cgi)
			$fields['cgi'] = "ON";
		else
			$fields['cgi'] = "OFF";
		if ($php)
			$fields['php'] = "ON";
		else
			$fields['php'] = "OFF";
		return $this->run($command, $fields, true);
	}

	public function createSubdomainUnderDomain($domain, $subdomain)
	{
		$command             = 'CMD_API_SUBDOMAINS';
		$fields['action']    = 'create';
		$fields['domain']    = $domain;
		$fields['subdomain'] = $subdomain;
		return urldecode($this->run($command, $fields));
	}

	public function createDatabase($databaseName, $databaseUser, $databasePass)
	{
		$command           = 'CMD_API_DATABASES';
		$fields['action']  = 'create';
		$fields['name']    = $databaseName;
		$fields['user']    = $databaseUser;
		$fields['passwd']  = $databasePass;
		$fields['passwd2'] = $databasePass;
		return urldecode($this->run($command, $fields));
	}

	public function getIPs()
	{
		$command = 'CMD_API_SHOW_RESELLER_IPS';
		return $this->run($command);
	}

	public function getUserList()
	{
		$command = 'CMD_API_SHOW_USERS';
		return urldecode($this->run($command));
	}

	public function getResellerList()
	{
		$command = 'CMD_API_SHOW_RESELLERS';
		return urldecode($this->run($command));
	}

	public function getAdminList()
	{
		$command = 'CMD_API_SHOW_ADMINS';
		return urldecode($this->run($command));
	}

	public function getUserUsage($name)
	{
		$command        = 'CMD_API_SHOW_USER_USAGE';
		$fields['user'] = $name;
		return urldecode($this->run($command, $fields));
	}

	public function getUserLimit($name)
	{
		$command        = 'CMD_API_SHOW_USER_CONFIG';
		$fields['user'] = $name;
		return urldecode($this->run($command, $fields));
	}

	public function getUserDomains($name)
	{
		$command        = 'CMD_API_SHOW_USER_DOMAINS';
		$fields['user'] = $name;
		return urldecode($this->run($command, $fields));
	}

	public function getCurrentAccountDomains()
	{
		$command = 'CMD_API_SHOW_DOMAINS';
		return urldecode($this->run($command));
	}

	public function getResellerPackageList()
	{
		$command = 'CMD_API_SHOW_USER_DOMAINS';
		return urldecode($this->run($command));
	}

	public function getResellerPackageDetail($name)
	{
		$command           = 'CMD_API_PACKAGES_RESELLER';
		$fields['package'] = $name;
		return urldecode($this->run($command, $fields));
	}

	public function getUserPackageList()
	{
		$command = 'CMD_API_PACKAGES_USER';
		return urldecode($this->run($command));
	}

	public function getUserPackageDetail($name)
	{
		$command           = 'CMD_API_PACKAGES_USER';
		$fields['package'] = $name;
		return urldecode($this->run($command, $fields));
	}

	public function getSubdomainListUnderDomain($domain)
	{
		$command          = 'CMD_API_SUBDOMAINS';
		$fields['domain'] = $domain;
		return urldecode($this->run($command, $fields));
	}

	public function getDatabaseList()
	{
		$command = 'CMD_API_DATABASES';
		return urldecode($this->run($command));
	}

	public function deleteSubdomainUnderDomain($domain, $subdomain, $delfiles)
	{
		$command           = 'CMD_API_SUBDOMAINS';
		$fields['action']  = "delete";
		$fields['domain']  = $domain;
		$fields['select0'] = $subdomain;
		if ($delfiles)
			$fields['contents'] = "yes";
		else
			$fields['contents'] = "no";
		return urldecode($this->run($command, $fields));
	}

	public function deleteDatabase($name)
	{
		$command           = 'CMD_API_DATABASES';
		$fields['action']  = "delete";
		$fields['select0'] = $name;
		return urldecode($this->run($command, $fields));
	}

	public function deleteAccount($name)
	{
		$command             = 'CMD_API_SELECT_USERS';
		$fields['confirmed'] = "Confirm";
		$fields['delete']    = "yes";
		$fields['select0']   = $name;
		return $this->run($command, $fields);
	}

	public function SuspendAccount($name)
	{
		$command            = 'CMD_API_SELECT_USERS';
		$fields['location'] = "CMD_SELECT_USERS";
		$fields['suspend']  = "Suspend";
		$fields['select0']  = $name;
		$this->run($command, $fields, true);
	}

	public function UnsuspendAccount($name)
	{
		$command            = 'CMD_API_SELECT_USERS';
		$fields['location'] = "CMD_SELECT_USERS";
		$fields['suspend']  = "Unsuspend";
		$fields['select0']  = $name;
		$this->run($command, $fields, true);
	}

	private function run($command, $fields = array(), $post = false)
	{
		$fieldstring = '';
		if (!$post) {
			foreach ($fields as $key => $value) {
				$fieldstring .= $key . "=" . urlencode($value) . "&";
			}
		}
		$authstr = $this->user . ":" . $this->pass;
		$ch      = curl_init();
		if ($this->usessl) {
			curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
			curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
			$url = "https://" . $this->host . ":2222/" . $command . "?" . $fieldstring;
		} else {
			$url = "http://" . $this->host . ":2222/" . $command . "?" . $fieldstring;
		}
		curl_setopt($ch, CURLOPT_URL, $url);
		if ($post) {
			curl_setopt($ch, CURLOPT_POST, 1);
			curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
		}
		curl_setopt($ch, CURLOPT_HEADER, 0);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
		$curlheaders[0] = "Authorization: Basic " . base64_encode($authstr);
		curl_setopt($ch, CURLOPT_HTTPHEADER, $curlheaders);
		curl_setopt($ch, CURLOPT_TIMEOUT, 60);
		$data = curl_exec($ch);
		if (curl_errno($ch)) {
			throw new \Exception("curl error:" . curl_errno($ch) . " - " . curl_error($ch));
		}
		curl_close($ch);
		if (strpos($data, "Sign in") == true) {
			throw new \Exception("DirectAdmin Login Failed");
		} else {
			if (strpos($data, "Your IP is blacklisted") !== false) {
				throw new \Exception("Server IP is Blacklisted");
			} else {
				return $data;
			}
		}
	}
}